package com.mediacenter.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.mediacenter.R;
import com.mediacenter.application.MApplication;
import com.mediacenter.site.model.Source;

public class OnlineActivity extends BaseActivity {

	private final String TAG = "OnlineActivity";
	private ListView mListView;

	private int[] listViewRes = null;
	private String[] listViewTitles = null;
	private MApplication application;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
//		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.layout_onlineactivity);
		application = MApplication.getInstance();

		findViews();
		setValues();
		setListeners();
	}

	private void findViews() {
		mListView = (ListView) this.findViewById(R.id.online_listview);
	}

	private void setValues() {

		mListView.setAdapter(new ListViewAdapter(this));

	}

	private void setListeners() {
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = null;
				Source source = null;
				switch (position) {
				case 0:
					
				case 1:
					
				case 2:
					
				case 3:
					source =  application.getSources().get(position);
					application.setCurrentSource(source);
					intent = new Intent(OnlineActivity.this, ListActivity.class);
					startActivity(intent);
					break;
				case 4:
					intent = new Intent(OnlineActivity.this, OnlineTVActivity.class);
					startActivity(intent);
					break;
				default:
					break;
				}
			}
		});
	}

	private final class ListViewHolder {
		public ImageView thumbnail;
		public TextView online_title;

	}

	private class ListViewAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
		private Context mContext;

		public ListViewAdapter(Context ctx) {
			this.mContext = ctx;
			this.mInflater = LayoutInflater.from(ctx);
			listViewRes = new int[] {R.drawable.logo_youku, R.drawable.logo_qq , R.drawable.logo_sohu, R.drawable.logo_pptv,R.drawable.logo_cntv/*R.drawable.logo_56, R.drawable.logo_iqiyi, R.drawable.logo_letv, R.drawable.logo_sina, R.drawable.logo_tudou*/};
			listViewTitles = new String[] { "优酷视频","腾讯视频","搜狐视频","PPTV","电视直播"/*, "56.com", "爱奇异", "乐视TV",   "新浪视频",  "土豆视频"*/ };
		}

		@Override
		public int getCount() {
			return listViewRes.length;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup arg2) {
			ListViewHolder viewHolder;
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.fragment_online_item, null);
				viewHolder = new ListViewHolder();
				viewHolder.thumbnail = (ImageView) convertView.findViewById(R.id.online_thumbnail);
				viewHolder.online_title = (TextView) convertView.findViewById(R.id.online_title);
				viewHolder.thumbnail.setImageResource(listViewRes[position]);
				viewHolder.online_title.setText(listViewTitles[position]);
				convertView.setTag(viewHolder);
			} else {
				viewHolder = (ListViewHolder) convertView.getTag();
			}

			return convertView;
		}

	}
}